<template>
  <div class="app-container">
    <el-form :inline="true" class="demo-form-inline">
      <el-form-item>
        <el-input v-model="searchObj.hosname" placeholder="医院名称"/>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchObj.hoscode" placeholder="医院编号"/>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" @click="getHospitalSetList()">查询</el-button>
      <!-- 工具条 -->
      <div>
        <el-button type="danger" size="mini" @click="batchRemove()">批量删除</el-button>
      </div>
    </el-form>
    <!-- banner列表 -->
    <el-table :data="records" stripe style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"/>
      <el-table-column type="index" width="50" label="序号"/>
      <el-table-column prop="hosname" label="医院名称"/>
      <el-table-column prop="hoscode" label="医院编号"/>
      <el-table-column prop="apiUrl" label="api基础路径" width="200"/>
      <el-table-column prop="contactsName" label="联系人姓名"/>
      <el-table-column prop="contactsPhone" label="联系人手机"/>
      <el-table-column label="状态" width="80">
        <template slot-scope="scope">
          {{ scope.row.status === 1 ? '可用' : '不可用' }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280" align="center">
        <template slot-scope="scope">
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="removeDataById(scope.row.id)">删除
          </el-button>
          <el-button
            v-if="scope.row.status==0"
            type="primary"
            size="mini"
            icon="el-icon-delete"
            @click="lockHospSet(scope.row.id, 1)">解锁
          </el-button>
          <el-button
            v-if="scope.row.status==1"
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="lockHospSet(scope.row.id, 0)">锁定
          </el-button>
          <!-- 修改数据 -->
          <router-link :to="'/hospitalSet/edit/'+scope.row.id">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
          </router-link>
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页 -->
    <el-pagination
      :current-page="currentPage"
      :page-size="limit"
      :total="total"
      style="padding: 30px 0; text-align: center;"
      layout="total, prev, pager, next, jumper"
      @current-change="fetchData"
    />

  </div>
</template>

<script>
import hospsetApi from '../../api/hospset'
import {Message} from 'element-ui'

export default {
  // 定义数据模型
  data() {
    return {
      list: [],
      currentPage: 1,
      limit: 3,
      searchObj: {},
      records: [],
      total: 0,
      hosname: '',
      multipleSelection: [] // 批量选择中选择的记录列表
    }
  },
  /* 页面渲染成功后获取数据*/
  created() {
    this.fetchData()
  },
  methods: {
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      console.log(this.multipleSelection)
    },
    /* 加载页面数据*/
    fetchData(page = 1) {
      this.currentPage = page
      hospsetApi.indexHospList(1, 10).then(
        result => {
          this.total = result.data.data.total
          this.records = result.data.data.records
        }
      ).catch(err => {
        this.$message.error(err.message + '错误信息')
        console.log(err.message)
      })
    },
    /* 按条件查询首页数据*/
    getHospitalSetList() {
      const param = {
        hosname: this.searchObj.hosname,
        hoscode: this.searchObj.hoscode
      }
      hospsetApi.getHospList(1, 10, param).then(
        res => {
          this.total = res.data.data.total
          this.records = res.data.data.records
        }
      )
    },
    removeDataById(id) {
      hospsetApi.removeDataById(id).then(
        res => {
          if (res.data.code == 200) {
            Message({
              message: '删除成功',
              type: 'success',
              duration: 3 * 1000
            })
          }
        }
      )
      // 删除后重新查询出数据
      this.fetchData()
    },
    /* 锁定或解锁医院*/
    lockHospSet(id, status) {
      hospsetApi.lockHospSet(id, status).then(
        res => {
          if (res.data.code == 200) {
            Message({
              message: '操作成功',
              type: 'success',
              duration: 2 * 1000
            })
          }
        }
      )
      // 锁定后重新查询出数据
      this.fetchData()
    },
    batchRemove() {
      this.$confirm('是否批量删除选中的医院', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(
        () => {
          if (this.multipleSelection.length < 1) {
            this.$message.warning('请选择删除的医院', 'duration: 2 * 1000')
            return
          }
          var list = []
          for (let i = 0; i < this.multipleSelection.length; i++) {
            list.push(this.multipleSelection[i].id)
          }
          const param = {
            'ids': list
          }
          console.log(list)
          hospsetApi.batchRemove(param).then(
            res => {
              if (res.data.code == 200) {
                this.$message({
                  type: 'success',
                  message: '删除成功',
                  duration: 2 * 1000
                })
              }
            })
          // 删除后重新查询出数据
          this.fetchData()
        })
    }
  }
}
</script>
